﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Expo.Training.BRule;

public partial class LogoutSSO : Pagebase
{
    HttpHelper helper = HttpHelper.Instance;
    public int next = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        next = helper.GetInt("next");
        switch (next)
        {
            case 1:
                Response.Redirect(getSNSURL());
                break;
            case 2:
                Response.Redirect(string.Format("http://{0}", HttpContext.Current.Request.Url.Authority));
                break;
            case 3:
                Expo.Training.BRule.Member.Member.ClearMemberCookie();
                CookieManager.ClearCookie("Header");
                CookieManager.ClearCookie("Score");
                CookieManager.ClearCookie("OnlineSession");
                Response.Redirect("/portal/bm/default.aspx");
                break;
            default:
                //CookieManager.ClearCookie("MemberID");
                //CookieManager.ClearCookie("Password");
                //CookieManager.ClearCookie("LoginCount");
                //CookieManager.ClearCookie("LastLoginTime");
                Expo.Training.BRule.Member.Member.ClearMemberCookie();
                CookieManager.ClearCookie("Header");
                CookieManager.ClearCookie("Score");
                CookieManager.ClearCookie("OnlineSession");
                Response.Redirect(getSSOPopTokenURL());
                break;
        }
    }

    /// <summary>
    /// 获取SSO接口地址
    /// </summary>
    /// <returns></returns>
    private string getSSOURL()
    {
        string url = System.Configuration.ConfigurationManager.AppSettings["Passport.TokenService"];
        if (!string.IsNullOrEmpty(url))
        {
            url = string.Format("http://{0}", url.Replace("http://", "").Split('/')[0]);
        }
        return url;
    }

    /// <summary>
    /// 获取SSO清空主站凭证地址
    /// </summary>
    /// <returns></returns>
    private string getSSOPopTokenURL()
    {
        string StrUrl = string.Format("{0}", HttpContext.Current.Request.Url);
        string url = getSSOURL() + "/poptoken.aspx?BackURL=" + StrUrl + "?next=1";
        return url;
    }

    /// <summary>
    /// 获取SNS接口地址
    /// </summary>
    /// <returns></returns>
    private string getSNSURL()
    {
        string url = System.Configuration.ConfigurationManager.AppSettings["BlogSNSUrl"];
        string StrUrl = string.Format("http://{0}", HttpContext.Current.Request.Url.ToString().Replace("http://", "").Split('?')[0]);
        if (!string.IsNullOrEmpty(url))
        {
            url = string.Format("http://{0}/{1}/{2}?BackURL={3}?next=2", url.Replace("http://", "").Split('/')[0], "Main", "LogoutSSO.aspx", StrUrl);
        }
        return url;
    }
}
